home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 2_2.lha / 2_2 / lsqrt.c < prev    next >
Text File  |  1993-08-08  |  578b  |  24 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. include <complex.h>
  6. omplex lsqrt(complex z)
  7.  
  8.    double x = real(z);
  9.    double y = imag(z);
  10.    // convert to polar and take root
  11.    //       2    2  1/2
  12.    // r = (x  + y )
  13.    //
  14.    // theta = O = arc tan (y / x)
  15.    //
  16.    //  1/2    1/2
  17.    // z    = r   (cos O/2 + i sin O/2)
  18.    double root_r = sqrt(sqrt(x * x +
  19.               y * y));
  20.    double half_t = atan2(y, x) / 2.0;
  21.    return complex(root_r * cos(half_t),
  22.        root_r * sin(half_t));
  23.  
  24.